package dao;


import modelo.Empresa;

import org.hibernate.Session;

public abstract class GenericDao<T> {
	private Session session;
	
	public GenericDao() {
		session =  Database.getInstance().getSession();
	}
	
	public GenericDao( Session session ){
		this.session = session;
	}
	
	public void incluiOuAtualiza( T obj){
		session.saveOrUpdate(obj);
	}
	
	public void remove(T obj){
		session.delete(obj);
	}

	protected Session getSession() {
		if(session == null){
			session = Database.getInstance().getSession();
		}
		return session;
	}

	protected void setSession(Session session) {
		this.session = session;
	}
	
	public void closeSession(){
		if(session !=null)
			session.close();
	}
	protected T buscarPorChavePrimaria(Class classe, int id){
		return (T)session.get(classe, id);
	}

	public abstract T buscarPorChavePrimaria(int id) ;
	
}
